using System;
using System.Collections.Generic;
using System.Configuration;
using System.Text;
using System.Data.Sql;
using System.Data.SqlClient;
using System.Data;
using System.Windows.Forms;
using System.Diagnostics;


namespace CustomReportEngine.Util
{
    public class ComboBoxItem
    {
        private string _value;

        public string Value
        {
            get { return _value; }
            set { _value = value; }
        }
        private string _label;

        public string Label
        {
            get { return _label; }
            set { _label = value; }
        }

        public ComboBoxItem(string v, string l)
        {
            _value = v;
            _label = l;
        }

        public override string ToString()
        {
            return Label;
        }
    }

    public class ConnectionManager
    {
        public static string ConnectionString = ConfigurationManager.ConnectionStrings["CustomReportEngine.Properties.Settings.PipelineConnectionString"].ConnectionString;

        public static List<ComboBoxItem> GetDealTypesList()
        {
            return GetList("SELECT list_loantype_label, list_loantype_value FROM list_loantype ORDER BY list_loantype_label");
        }

        public static List<ComboBoxItem> GetStatusList()
        {
            return GetList("SELECT CAST(dbo.table_order.table_order_order AS VARCHAR(5)) + '. ' + dbo.status.status_name AS status_name, CAST(dbo.status.status_id AS VARCHAR(5)) AS status_id  FROM dbo.status INNER JOIN  dbo.table_order ON dbo.status.status_id = dbo.table_order.table_order_status_id WHERE (dbo.table_order.table_order_name = N'default') ORDER BY dbo.table_order.table_order_order");
        }

		public static List<ComboBoxItem> GetDealSubTypesList(string filter)
		{
			return GetList("SELECT DISTINCT deal_sub_type2, deal_sub_type2 FROM deal WHERE deal_sub_type2 IS NOT NULL AND deal_sub_type2 <> '' "+filter+" ORDER BY deal_sub_type2");
		}

        public static List<ComboBoxItem> GetList(string sql)
        {
            SqlDataReader dr = null;
            List<ComboBoxItem> items = new List<ComboBoxItem>();
            try
            {
                SqlConnection cn = new SqlConnection(ConnectionString);
                cn.Open();
                SqlCommand cmd = new SqlCommand(sql, cn);
                dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

                while (dr.Read())
                    items.Add(new ComboBoxItem(dr.GetString(1), dr.GetString(0)));

                dr.Close();
            }
            catch (Exception e)
            {
                // do nothing
            }
            finally
            {
                if (dr != null && !dr.IsClosed)
                    dr.Close();
            }
            return items;
        }
    }

	public class ErrorLog
	{
		public static void Write(string name, Exception e) {
			string sSource;
			string sLog;

			sSource = "GS ReportViewer";
			sLog = "Application";
			string sMessage = e.Message + Environment.NewLine + "------------------- " + 
				Environment.NewLine + e.StackTrace.ToString();

			if (!EventLog.SourceExists(sSource))
				EventLog.CreateEventSource(sSource,sLog);

			EventLog.WriteEntry(sSource, "GS ReportViewer Exception: " + name + 
				Environment.NewLine + sMessage,
				EventLogEntryType.Error, 1);

			ErrorPopup popup = new ErrorPopup(sMessage);
			popup.ShowDialog();
		}
	}
}
